/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2021-01-05
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "rwip_config.h"     		// SW configuration
#include "user_config.h"

#if(AIS_TMALL_GENIE_ENABLE)

#include <string.h>
#include "app.h"                     // Application Definitions
#include "app_task.h"                // application task definitions
#include "ais.h"
#include "app_ais.h"                // BLPS Application Module Definitions
#include "ais_task.h"               // health thermometer functions
#include "gattc_task.h"
#include "co_bt.h"
#include "co_math.h"
#include "co_utils.h"
#include "arch.h"                    // Platform Definitions
#include "prf.h"
#include "user.h"
#include "ke_timer.h"
#include "prf_types.h"               // Profile common types definition

/*----------------------------macro file---------------------------*/
#define AIS_FMASK_VAL	0x18
#define AIS_BOND_MASK	0x20
#define AIS_ROPE_PID	0x09, 0x76, 0x65, 0x00	//0x00657609		//test 0xD4, 0xA1, 0x65, 0x00

#define AIS_ADV_EXT_UPLOAD_BITMASK	0x0100

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/
struct ais_env_tag app_ais_env;

typedef struct {
	uint8_t data[23];
	uint16_t ext_mask;
} ais_adv_t;

static uint8_t aisAdvertData[25] = 
{
	// flags
	0x02,
	GAP_AD_TYPE_FLAGS,
	GAP_LE_GEN_DISCOVERABLE_FLG | GAP_BR_EDR_NOT_SUPPORTED,
	// service UUIDs
	0x03,
	GAP_AD_TYPE_MORE_16_BIT_UUID,
	LO_UINT16(AIS_PRIMARY_SVC_UUID),
	HI_UINT16(AIS_PRIMARY_SVC_UUID),
	// manufacture data
	0x11,
	GAP_AD_TYPE_MANU_SPECIFIC_DATA,
	LO_UINT16(0x01A8),
	HI_UINT16(0x01A8),
	0xB5,				//Subtype: BLE GATT,  VID: 5
	AIS_FMASK_VAL,		//BLE4.0, No OTA, No Security, one dev one key, unpair state
	AIS_ROPE_PID,		//Product ID
	0, 0, 0, 0, 0, 0,	//BLE MAC
	0, 0,				//EXT MASK
};

static uint8_t aisScanData[ADV_DEVICE_NAME_LEN+2] =
{
	// local name
	(ADV_DEVICE_NAME_LEN+1),
	GAP_AD_TYPE_COMPLETE_NAME,
};

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

void app_ais_tmall_genie_mac_get(uint8_t *mac, uint8_t len)
{
	uint8_t mac_info[] = {0x51, 0x50, 0x11, 0x6C, 0x14, 0x18};	//{0xC8, 0x12, 0x10, 0x6C, 0x14, 0x18}
	memcpy(mac, mac_info, 6);
}


void app_ais_bind_state_adv_set(bool bind)
{
	ais_adv_t *adv = (ais_adv_t*)aisAdvertData;

	memcpy(&aisAdvertData[17], co_default_bdaddr.addr, sizeof(co_default_bdaddr.addr));
	memcpy(&aisScanData[2], ADV_DEVICE_NAME, ADV_DEVICE_NAME_LEN);
	if(bind) {
		aisAdvertData[12] = AIS_FMASK_VAL;
		adv->ext_mask &= ~((uint16_t)AIS_ADV_EXT_UPLOAD_BITMASK);
	}
	else {
		aisAdvertData[12] = AIS_FMASK_VAL | AIS_BOND_MASK;
	}
	//GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(aisAdvertData), aisAdvertData);	//TODO
	//GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(aisScanData), aisScanData);	//TODO
}


void app_ais_data_upload_valid_adv_set(bool valid)
{
	ais_adv_t *adv = (ais_adv_t*)aisAdvertData;

	if(valid) {
		adv->ext_mask |= (uint16_t)AIS_ADV_EXT_UPLOAD_BITMASK;
	}
	else {
		adv->ext_mask &= ~((uint16_t)AIS_ADV_EXT_UPLOAD_BITMASK);
	}
	//GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(aisAdvertData), aisAdvertData);	//TODO
}



void app_ais_init(void)
{
    // Reset the environment
    memset(&app_ais_env, 0, sizeof(struct ais_env_tag));
}

void app_ais_add_sensor(void)
{
	struct ais_db_cfg *db_cfg;
    // Allocate the AIS_CREATE_DB_REQ
    struct gapm_profile_task_add_cmd *req = KE_MSG_ALLOC_DYN(GAPM_PROFILE_TASK_ADD_CMD,
                                                  TASK_GAPM, TASK_APP,
                                                  gapm_profile_task_add_cmd, sizeof(struct ais_db_cfg));
    // Fill message
    req->operation   = GAPM_PROFILE_TASK_ADD;
    req->sec_lvl     = 0;
    req->prf_task_id = TASK_ID_AIS;
    req->app_task    = TASK_APP;
    req->start_hdl   = 0;

	// Set parameters
	db_cfg = (struct ais_db_cfg* )req->param;
	// Sending of notifications is supported
	db_cfg->features = 0;

    // Send the message
    ke_msg_send(req);
}

static int app_ais_msg_dflt_handler(ke_msg_id_t const msgid,
                                     void const *param,
                                     ke_task_id_t const dest_id,
                                     ke_task_id_t const src_id)
{
    // Drop the message
    return (KE_MSG_CONSUMED);
}


/*
 * LOCAL VARIABLE DEFINITIONS
 ****************************************************************************************
 */

/// Default State handlers definition
const struct ke_msg_handler app_ais_msg_handler_list[] =
{
    // Note: first message is latest message checked by kernel so default is put on top.
    {KE_MSG_DEFAULT_HANDLER,        (ke_msg_func_t)app_ais_msg_dflt_handler},
};

const struct ke_state_handler app_ais_table_handler =
    {&app_ais_msg_handler_list[0], (sizeof(app_ais_msg_handler_list)/sizeof(struct ke_msg_handler))};


#endif	//#if(ROPE_CENTRAL_SUP_ENABLE)

